package com.bestcem.xm.member.entity.mongo;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.bestcem.xm.member.enums.ExtendFieldTypeEnum;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Date;

/**
 * 联系人扩展字段
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/7/18
 */

@Getter
@Setter
public class ExtendFieldValue implements Serializable {

    @JsonProperty(value = "field_id")
    @JSONField(name = "field_id")
    private String fieldId;

    @JsonProperty(value = "field_value")
    @JSONField(name = "field_value")
    private Object fieldValue;

    @JsonProperty(value = "f_type")
    @JSONField(name = "f_type")
    private Integer fieldType;

    @JsonProperty(value = "field_name")
    @JSONField(name = "field_name")
    private String fieldName;

    /**
     * 重构set方法  修改返回类型
     *
     * @param fieldValue
     */
    public void setFieldValue(Object fieldValue) {
        if (fieldType == ExtendFieldTypeEnum.DATETIME.getIndex()) {
            // 日期
            if (ObjectUtil.isEmpty(fieldValue)) {
                this.fieldValue = "";
            } else {
                String date = String.valueOf(fieldValue);
                this.fieldValue =  date;
            }
        } else if (fieldType == ExtendFieldTypeEnum.NUMBER.getIndex()) {
            // 数字
            if (ObjectUtil.isEmpty(fieldValue)) {
                this.fieldValue = null;
            } else {
                String number = String.valueOf(fieldValue);
                if (NumberUtil.isDouble(number)) {
                    this.fieldValue = Double.valueOf(number);
                } else {
                    this.fieldValue = Integer.valueOf(number);
                }
            }
        } else {
            // 字符串
            if (ObjectUtil.isEmpty(fieldValue)) {
                this.fieldValue = "";
            } else {
                this.fieldValue = String.valueOf(fieldValue);
            }
        }
    }

    // fixme  后续需要修改
    public void setDateValue(Date date) {
        this.fieldValue = date;
    }
}
